package assfur.eran.logic;

import assfur.eran.config.ConfigManger;
import assfur.eran.utils.FileUtils;
import assfur.eran.utils.SVNWrap;

import java.io.File;

public class SVNAutoFolder {

    private SVNWrap mSVNWrap;

    public void run() {

        mSVNWrap = new SVNWrap();
        mSVNWrap.initialize();

        File rootFile = new File(ConfigManger.getInstance().getMeta().inputPath);
        scanEachFileUnderFolder(rootFile);
    }


    private void scanEachFileUnderFolder(File _folder) {
        for (File subFile : _folder.listFiles()) {
            if (subFile.isDirectory()) {
                //忽略.svn目录
                if (!subFile.getName().equals(".svn")) {
                    scanEachFileUnderFolder(subFile);
                }
            } else {
                String relatePath = FileUtils.getFileRelatePath(subFile);
                String fileVersion = mSVNWrap.getFileSVNVersion(relatePath.substring(1, relatePath.length()).replaceAll("\\\\", "/"));

                String pr = relatePath.replace(subFile.getName(), "");
                String outputPath = ConfigManger.getInstance().getMeta().outputPath + pr + fileVersion + "\\" + subFile.getName();
                File newFile = new File(outputPath);
                if (!newFile.exists()) {
                    newFile.getParentFile().mkdirs();
                    FileUtils.copyFile(subFile, newFile);
                }
            }
        }
    }

}
